package com.woniu.service.impl;

import com.woniu.dao.LessonDao;
import com.woniu.pojo.Lesson;
import com.woniu.pojo.LessonExample;
import com.woniu.pojo.PageBean;
import com.woniu.service.LessonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LessonServiceImpl implements LessonService {
    @Resource
    private LessonDao lessonDao;

    /**
     * 查询列表
     * @param pageBean
     * @param lesson
     * @return
     */
    @Override
    public List<Lesson> getLessonList(PageBean pageBean, Lesson lesson) {
        LessonExample lessonExample =new LessonExample();
        LessonExample.Criteria criteria=lessonExample.createCriteria();
        if (lesson.getCid()!=null&&!("").equals(lesson.getCid())){
            criteria.andCidEqualTo(lesson.getCid());
        }
        if (lesson.getName()!=null&&!("").equals(lesson.getName())){
            criteria.andNameEqualTo(lesson.getName());
        }
        Long countRow = lessonDao.countByExample(lessonExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage = pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);

        lessonExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        lessonExample.setLimit(pageBean.getPageSize());
        lessonExample.setOrderByClause("btime desc");
        List<Lesson> lessonList = lessonDao.selectByExample(lessonExample);
        return lessonList;
    }

    /**
     * 添加
     * @param lesson
     */
    @Override
    public void saveLesson(Lesson lesson) {
     lessonDao.insertSelective(lesson);
    }

    /**
     * 删除
     * @param lno
     */
    @Override
    public void delByLesson(Integer lno) {
        lessonDao.deleteByPrimaryKey(lno);
    }
}
